import { showNotify, showConfirmDialog, showToast } from 'vant';
import { useCookies } from 'vue3-cookies';
const { cookies } = useCookies();
import Request from '../services/request';
import { ref } from 'vue';

const warnNotify = (option) => {

    let message = '未知信息';
    let duration = 1500;
    let fn = () => {}

    if(typeof option === 'object')
    {
        message = option.message ?? message;
        duration = option.duration ?? duration;
        fn = typeof option.onClose === 'function' ? option.onClose : fn;
    }else if(typeof option === 'string')
    {
        message = option ?? message
    }

    showNotify({
        message:message,
        type:'warning',
        duration:duration,
        onClose:fn
    });
}

const successNotify = (option) => {

    let message = '未知信息';
    let duration = 1500;
    let fn = () => {}

    if(typeof option === 'object')
    {
        message = option.message ?? message;
        duration = option.duration ?? duration;
        fn = typeof option.onClose === 'function' ? option.onClose : fn;
    }else if(typeof option === 'string')
    {
        message = option ?? message
    }

    showNotify({
        message:message,
        type:'success',
        duration:duration,
        onClose:fn
    });
}


const install = (app) => {

    const login = async () => {
        let business = cookies.get('business') ? cookies.get('business') : {};
    
        if(!business || business.hasOwnProperty('id') === false)
        {
    
            return {code:0,msg:'请登录',data:null};
        }
    
        let id = business.id ?? 0;
        let mobile = business.mobile ?? '';
    
        let result = await Request.POST({
            url:'business/base/check',
            params:{
                id,
                mobile
            }
        })

        if(result.code === 1)
        {
            cookies.set('business',result.data);
            app.config.globalProperties.$business = ref(cookies.get('business')) ?? {};
        }
    
        return result;   
    }
    

    app.config.globalProperties.$warnNotify = warnNotify;
    app.config.globalProperties.$successNotify = successNotify;
    app.config.globalProperties.$login = login;
    app.config.globalProperties.$showConfirm = showConfirmDialog;
    app.config.globalProperties.$business = ref(cookies.get('business')) ?? {};
    app.config.globalProperties.$showToast = showToast;
}

export default {
    install,
    warnNotify,
    successNotify,
}